package org.mspring.mlog.web.module.web.ucenter.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.core.ServiceFactory;
import org.mspring.mlog.entity.Comment;
import org.mspring.mlog.entity.security.User;
import org.mspring.mlog.service.CommentService;
import org.mspring.mlog.web.query.CommentQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.utils.RequestUtils;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * 
 * @author Gao Youbo
 * @since 2013年7月12日
 */
@Controller
@RequestMapping("/u/{username}/admin/comment")
public class UCenterAdmin_CommentWidget extends Abstract_UCenterAdminWidget {

    @Autowired
    private CommentService commentService;

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping("/list")
    public String list(@ModelAttribute Page<Comment> commentPage, @ModelAttribute Comment comment, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (spaceUser == null) {
            return "skin:/error/404";
        }
        if (commentPage == null) {
            commentPage = new Page<Comment>();
        }
        if (comment == null) {
            comment = new Comment();
        }
        commentPage.setSort(new Sort("id desc", ""));
        if (queryParams == null) {
            queryParams = new HashMap();
        }
        queryParams.put("toUser.id", spaceUser.getId());
        commentPage = commentService.listPage(new CommentQueryCriterion(queryParams), commentPage);
        model.addAttribute("commentPage", commentPage);
        return "skin:/ucenter/admin/comment/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseEntity delete(HttpServletRequest request, HttpServletResponse response, Model model) {
        ResponseEntity rsp = new ResponseEntity();
        Long id = RequestUtils.getRequestParameterAsLong(request, "id");
        if (id == null) {
            rsp.setSuccess(false);
            rsp.setMessage("无法确定要删除的评论");
            return rsp;
        }
        try {
            commentService.safeRemove(id, spaceUser.getId());
            rsp.setSuccess(true);
            return rsp;
        } catch (Exception e) {
            rsp.setSuccess(false);
            rsp.setMessage(e.getMessage());
            return rsp;
        }
    }

    /**
     * 回复评论页面
     * 
     * @param user
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/reply")
    public String reply(HttpServletRequest request, HttpServletResponse response, Model model) {
        Long id = RequestUtils.getRequestParameterAsLong(request, "id");
        model.addAttribute("id", id);
        return "skin:/ucenter/admin/comment/reply";
    }

    /**
     * 提交回复
     * 
     * @param user
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/reply/do")
    @ResponseBody
    public ResponseEntity reply_do(HttpServletRequest request, HttpServletResponse response, Model model) {
        ResponseEntity rsp = new ResponseEntity();
        Long id = RequestUtils.getRequestParameterAsLong(request, "id");
        if (id == null) {
            rsp.setSuccess(false);
            rsp.setMessage("回复评论失败，参数错误");
            return rsp;
        }
        Comment parentCmt = commentService.get(id);
        if (parentCmt == null) {
            rsp.setSuccess(false);
            rsp.setMessage("无法找到被回复的评论");
            return rsp;
        }
        String fkey = parentCmt.getFkey();
        String ftype = parentCmt.getFtype();
        User toUser = parentCmt.getAuthor();

        String content = RequestUtils.getRequestParameterAsString(request, "content");
        String ip = RequestUtils.getRemoteIP(request);
        String userAgent = RequestUtils.getUserAgent(request);
        String status = Comment.Status.APPROVED;
        String is_comment_audit = ServiceFactory.getOptionService().getOption("comment_audit");
        if ("true".equals(is_comment_audit)) {
            status = Comment.Status.WAIT_FOR_APPROVE;
        }

        // 验证数据
        if (toUser == null) {
            rsp.setSuccess(false);
            rsp.setMessage("评论失败，无法确定toUser参数");
            return rsp;
        }
        if (StringUtils.isBlank(fkey)) {
            rsp.setSuccess(false);
            rsp.setMessage("评论框配置错误，请确认参数f_key是否正确配置");
            return rsp;
        }
        if (StringUtils.isBlank(ftype)) {
            rsp.setSuccess(false);
            rsp.setMessage("评论框配置错误，请确认参数f_type是否正确配置");
            return rsp;
        }
        if (StringUtils.isBlank(content)) {
            rsp.setSuccess(false);
            rsp.setMessage("请输入评论内容");
            return rsp;
        }

        // 构建评论对象
        Comment cmt = new Comment();
        cmt.setAuthor(spaceUser);
        cmt.setContent(content);
        cmt.setPostIp(ip);
        cmt.setAgent(userAgent);
        cmt.setFkey(fkey);
        cmt.setFtype(ftype);
        cmt.setStatus(status);
        cmt.setToUser(toUser);
        cmt.setParent(new Comment(id));

        try {
            commentService.save(cmt);
            rsp.setSuccess(true);
            return rsp;
        } catch (Exception e) {
            rsp.setSuccess(false);
            rsp.setMessage(e.getMessage());
            return rsp;
        }
    }
}
